//index.js
//获取应用实例
const app = getApp()
var common = require("../../common.js")
Page({
  data: { gid: 0, detail: null, rotate:0,scale:1.6},
  onLoad: function (params) {
    this.data.gid=params.id;
    this.getGameDetail();
  },
  getGameDetail:function(){
    var that = this;
    wx.showLoading({
      title: '正在加载...',
    })
    common.request(
      {
        url: 'c=Game&a=getGameDetail&gid=' + this.data.gid,
        success: function (re) {
          var rotate=0;
          if(re.detail.type=='group'){
            rotate=-90;
          }
          that.setData({
            playlist: re.list,
            detail: re.detail,
            rotate:rotate
          });
          if (re.detail.hasPay != 1 || re.detail.audit != 1) {
            wx.hideShareMenu({});
          }

        },
        fail: function (re) { },
        complete: function (re) { wx.hideLoading(); }
      }
    );
  },
  onShareAppMessage:function(){
    return {
      title: '厉害了，找到"' + this.data.detail.target+'",还有机会赢取红包奖励',
      imageUrl:'../../image/f.jpg',
      path: 'pages/suren/begin?id='+this.data.gid,
      success: function (res) {
      },
      fail: function (ret) {
        console.log('cancelShare', ret);
      }
  }
  },
  refundTap:function(){
    var that=this;
    //退款
    wx.showModal({
      title: '提示',
      content: '确定退还余额吗？退回后，您的好友将不能获得红包，同时您也不能再分享本游戏',
      success:function(res){
        if(res.cancel){
          return false;
        }
        wx.showLoading({
          title: '正在提交请求',
        })
        common.request(
          {
            url: 'c=Game&a=refundGame&gid=' + that.data.gid,
            success: function (re) {
              if (re.status != 0) {
                wx.showModal({
                  title: '信息',
                  content: re.msg,
                });
              }else{
                that.getGameDetail();
                wx.showToast({
                  title: '退款成功',
                })
              }
            },
            fail: function (re) { },
            complete: function (re) { 
              wx.hideLoading();
            }
          }
        );
      }
    })
    

  },
  prizeTap:function(){
    //设置红包
    wx.navigateTo({
      url: 'money?gid=' + this.data.gid,
    })
  },
  faild:function(){
    wx.hideLoading();
    wx.showToast({
      title: '分享图创建失败',
    })
  },
//请求获得小程序码地址
  friendsTap:function(){
    var that=this;
    wx.showLoading({
      title: '正在创建分享图片',
    })
    common.request(
      {
        url: 'c=Game&a=getQRCode&gid=' + this.data.gid,
        success: function (re) {
          if(re.status==0){
            that.setData({
              qrcode: re.url
            });
            that.downloadQRCode();
          }else{
            that.faild();
          }
        },
        fail: function (re) { that.faild();},
        complete: function (re) {  }
      }
    );
  },

  downloadGroup:function(){
    var that=this;
    wx.downloadFile({
      url:that.data.detail.pic,
      success: function (res1) {
        //缓存头像图片
        that.setData({
          group: res1.tempFilePath
        });
        
        console.log('开始绘制合照图片')
        that.drawImageGroup();
        setTimeout(function () {
          that.canvasToImage()
        }, 200)

      },
      fail: function (re) { 
        that.faild();
      },
    });
  },
  //背景图下载
  downloadBg: function () {
    var that = this;
    wx.downloadFile({
      url: "https://findme.bibei365.com/Public/image/bg.jpg",
      success: function (res1) {
        //缓存头像图片
        that.setData({
          bg: res1.tempFilePath
        });
        that.downloadGroup();
      }
    });
  },
  downloadAvatar:function(){
    var that = this;
    wx.downloadFile({
      url: app.globalData.userInfo.avatarUrl,
      success: function (res1) {
        //缓存头像图片
        that.setData({
          portrait_temp: res1.tempFilePath
        });

        if(that.data.detail.type=='single'){
          console.log('开始绘制图片')
          that.drawImageSingle();
          setTimeout(function () {
            that.canvasToImage()
          }, 200)
        }else{
          that.downloadBg();
        }
      },
      fail:function(res){
        wx.showModal({
          title: '错误',
          content: "网络出错，请重试，如反复出错请在意见建议中反馈",
        })
      }
    });
  },
//下载小程序码图
  downloadQRCode:function(){
    var that=this;    
        //缓存canvas绘制小程序二维码
        wx.downloadFile({
          url: that.data.qrcode,
          success: function (res2) {
            console.log('二维码：' + res2.tempFilePath)
            //缓存二维码
            that.setData({
              qrcode_temp: res2.tempFilePath
            })
            that.downloadAvatar();
          }
        })
  },
  drawImageSingle: function () {
    //绘制canvas图片
    var that = this
    const ctx = wx.createCanvasContext('myCanvas')
    var bgPath = "../../image/f.jpg";
    var portraitPath = that.data.portrait_temp
    var hostNickname = app.globalData.userInfo.nickName

    var qrPath = that.data.qrcode_temp
    var windowWidth = app.globalData.windowWidth;
    var windowHeight = windowWidth * 0.8+130;
    that.data.windowHeight = windowHeight;
    var headR = windowWidth / 10;
    //绘制背景图片

    // that.tmp(ctx);
    // return;
    ctx.setFillStyle("#eee4da")
    ctx.fillRect(0, 0, windowWidth,200)
    ctx.stroke();
    ctx.drawImage(bgPath, 0, 130, windowWidth, windowHeight-130);
   // ctx.drawImage(bgPath, 10, 3 * headR + 10, windowWidth - 20, windowHeight - 3 * headR - 20);

    //绘制头像
    ctx.save()
    ctx.beginPath()
    ctx.arc(1.38 * headR + 10, 1.38 * headR + 10, 1.38 * headR, 0, 2 * Math.PI)
    ctx.closePath();

    ctx.clip()
    //ctx.drawImage(portraitPath, 0.7 * windowWidth / 2, 0.17 * windowWidth, 0.3 * windowWidth, 0.3 * windowWidth)
    ctx.drawImage(portraitPath, 10, 10, 2.76 * headR, 2.76 * headR)
    ctx.restore()


    //绘制二维码
    //ctx.drawImage(qrPath, 0.64 * windowWidth / 2, 0.75 * windowWidth, 0.36 * windowWidth, 0.36 * windowWidth)
    ctx.drawImage(qrPath, windowWidth - 3 * headR - 10, 5, 3 * headR, 3 * headR)

    ctx.strokeStyle = 'white';//边框颜色
    ctx.lineWidth = '2';
    //绘制昵称
    ctx.setFillStyle('#000')
    ctx.setFontSize(0.055 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText(hostNickname, windowWidth / 2, headR + 10);
    ctx.fillText(hostNickname, windowWidth / 2, headR + 10);

    //我发布了红包活动
    ctx.setFillStyle('#333')
    ctx.setFontSize(0.037 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText('我发布了红包活动', windowWidth / 2, 2.0 * headR)
    ctx.fillText('我发布了红包活动', windowWidth / 2, 2.0 * headR)

    //长按二维码领红包
    ctx.setFillStyle('#333')
    ctx.setFontSize(0.04 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText('长按二维码领红包', windowWidth / 2, 2.7 * headR)
    ctx.fillText('长按二维码领红包', windowWidth / 2, 2.7 * headR)

    
    ctx.draw();
  },
  drawImageGroup:function() {
    //绘制canvas图片
    var that = this
    const ctx = wx.createCanvasContext('myCanvas')
    var bgPath = that.data.bg;
    var groupPath = that.data.group;
    var portraitPath = that.data.portrait_temp
    var hostNickname = app.globalData.userInfo.nickName

    var qrPath = that.data.qrcode_temp
    var windowWidth = app.globalData.windowWidth;
    var windowHeight = that.data.scale * windowWidth;
    that.data.windowHeight = windowHeight;
    var headR = windowWidth/10;
    //绘制背景图片

    // that.tmp(ctx);
    // return;


    ctx.drawImage(bgPath, 0, 0, windowWidth, windowHeight);
    ctx.drawImage(groupPath, 10, 3*headR+10, windowWidth-20, windowHeight-3*headR-20);

    //绘制头像
    ctx.save()
    ctx.beginPath()
    ctx.arc(1.38*headR+10, 1.38*headR+10, 1.38*headR, 0, 2 * Math.PI)
    ctx.closePath();

    ctx.clip()
    //ctx.drawImage(portraitPath, 0.7 * windowWidth / 2, 0.17 * windowWidth, 0.3 * windowWidth, 0.3 * windowWidth)
    ctx.drawImage(portraitPath,  10, 10, 2.76 * headR, 2.76 * headR)
    ctx.restore()


    //绘制二维码
    //ctx.drawImage(qrPath, 0.64 * windowWidth / 2, 0.75 * windowWidth, 0.36 * windowWidth, 0.36 * windowWidth)
    ctx.drawImage(qrPath, windowWidth - 3 * headR - 10, 5, 3 * headR, 3 * headR)

    ctx.strokeStyle = 'white';//边框颜色
    ctx.lineWidth = '2';
    //绘制昵称
    ctx.setFillStyle('#000')
    ctx.setFontSize(0.055 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText(hostNickname, windowWidth / 2, headR + 10);
    ctx.fillText(hostNickname, windowWidth / 2, headR + 10);
    
    //我发布了红包活动
    ctx.setFillStyle('#333')
    ctx.setFontSize(0.037 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText('我发布了红包活动', windowWidth / 2, 2.0 * headR)
    ctx.fillText('我发布了红包活动', windowWidth / 2, 2.0 * headR)

    //长按二维码领红包
    ctx.setFillStyle('#333')
    ctx.setFontSize(0.04 * windowWidth)
    ctx.setTextAlign('center')
    ctx.strokeText('长按二维码领红包', windowWidth / 2, 2.7 * headR)
    ctx.fillText('长按二维码领红包', windowWidth / 2, 2.7*headR)

    ctx.strokeStyle = '#888';//边框颜色
    ctx.setFillStyle('#ffffff')
    ctx.setFontSize(0.047 * windowWidth)
    ctx.setTextAlign('left')
    var begin = 0.47;
    var step = 0.1
    var left =120;
    ctx.strokeText('那时的我们拥有', left, begin * windowWidth)
    ctx.fillText('那时的我们拥有', left, begin * windowWidth)
    begin +=step;
    ctx.strokeText('没有污染过的清晨', left, begin * windowWidth)
    ctx.fillText('没有污染过的清晨', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('嘀嘀嗒嗒的秒针', left, begin * windowWidth)
    ctx.fillText('嘀嘀嗒嗒的秒针', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('却留不住一个黄昏', left, begin * windowWidth)
    ctx.fillText('却留不住一个黄昏', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('……', left, begin * windowWidth)
    ctx.fillText('……', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('怀念啊我们的青春啊', left, begin * windowWidth)
    ctx.fillText('怀念啊我们的青春啊', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('留下的脚印拼成一幅画', left, begin * windowWidth)
    ctx.fillText('留下的脚印拼成一幅画', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('最美的风景是你的笑容', left, begin * windowWidth)
    ctx.fillText('最美的风景是你的笑容', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('那一句再见有太多的放不下', left, begin * windowWidth)
    ctx.fillText('那一句再见有太多的放不下', left, begin * windowWidth)
    begin += step;
    ctx.strokeText('——《怀念青春》', windowWidth/2, begin * windowWidth)  
    ctx.fillText('——《怀念青春》', windowWidth / 2, begin * windowWidth)  
    ctx.draw();
  },



  canvasToImage:function() {
    var that = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: app.globalData.windowWidth,
      height: that.data.windowHeight ,
      destWidth: app.globalData.windowWidth * 4,
      destHeight: that.data.windowHeight * 4 ,
      canvasId: 'myCanvas',
      success: function (res) {
        console.log('朋友圈分享图生成成功:' + res.tempFilePath)
        wx.hideLoading();

        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success:function(rt){
            console.log(rt);
            wx.previewImage({
              current: res.tempFilePath, // 当前显示图片的http链接
              urls: [res.tempFilePath] // 需要预览的图片http链接列表
            })
          },
          fail:function(err){
            if (err.errMsg == "saveImageToPhotosAlbum:fail cancel") {
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
          }
          }
        })
        // wx.saveFile({
        //   tempFilePath: res.tempFilePath,
        //   success: function success(res) {
        //     console.log('saved::' + res.savedFilePath);
        //   },
        //   complete: function fail(e) {
        //     console.log(e.errMsg);
        //   }
        // });

        
      },
      fail: function (err) {
        console.log('失败')
        console.log(err)
      }
    })
  },

tmp:function(ctx){
  var context = ctx;
  context.fillStyle = "#FFF";
  context.fillRect(0, 0, 800, 600);

  //在屏幕上绘制一个大方块    
  context.fillStyle = "black";
  context.fillRect(10, 10, 200, 200);
  context.save();
  context.beginPath();

  //裁剪画布从(0，0)点至(50，50)的正方形    
  context.fillRect(30, 30, 550, 100);
  context.clip();

  //红色圆    
  context.beginPath();
  context.strokeStyle = "red";
  context.lineWidth = 5;
  context.arc(100, 100, 100, 0, Math.PI * 2, false);
  //整圆    
  context.stroke();
  context.closePath();

  context.restore();

  //再次裁切整个画布    
  context.beginPath();
  context.rect(0, 0, 500, 500);
  context.clip();

  //绘制一个没有裁切的蓝线    
  context.beginPath();
  context.strokeStyle = "blue";
  context.lineWidth = 5;
  context.arc(100, 100, 50, 0, Math.PI * 2, false);
  //整圆    
  context.stroke();
  context.closePath();
  context.draw();
  return;
}
})




